<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>
<style>
    @media (min-width: 576px) {
        .card-columns {
            column-count: 1;
        }
    }
    @media (min-width: 768px) {
        .card-columns {
            column-count: 2;
        }
    }
    @media (min-width: 992px) {
        .card-columns {
            column-count: 2;
        }
    }
    @media (min-width: 1200px) {
        .card-columns {
            column-count: 3;
        }
    }
</style>
<div class="container-fluid bg-white">

    <?php if (FALSE === $data_verify) { ?>
    <div class="row my-3 py-3">
        <div class="col-md-4 mx-auto">
            <p class="text-danger text-center font-weight-bold">
                <?php echo lang('plugin_login_tips');?>
            </p>
            <div class="ajax_modal_body">
                <form action="<?php echo url('plugin-read', '', TRUE);?>" method="post" id="form">
                    <div class="form-group input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="icon icon-user icon-fw"></i></span>
                        </div>
                        <input type="text" class="form-control" placeholder="<?php echo lang('email');?> / <?php echo lang('username');?>" id="email" name="email">
                        <div class="invalid-feedback"></div>
                    </div>
                    <div class="form-group input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="icon icon-lock icon-fw"></i></span>
                        </div>
                        <input type="password" class="form-control" placeholder="<?php echo lang('password');?>" id="password" name="password">
                        <div class="invalid-feedback"></div>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-danger btn-block" id="submit" data-loading-text="<?php echo lang('submiting');?>...">
                            <?php echo lang('login');?>
                        </button>
                    </div>
                    <div class="media">
                        <div class="media-body text-right">
                            <a target="_blank" href="http://www.wellcms.cn/user-create.html" class="text-muted">
                                <small>
                                    <?php echo lang('user_create');?>
                                </small>
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <p class="text-muted font-weight-bold my-3">
                Local mode does not allow login / 本地模式不允许登录
            </p>
        </div>
    </div>
    <?php } else { ?>

    <div class="d-flex justify-content-between thread py-3">
        <div class="btn-group" role="group">
            <a role="button" class="ajax btn btn-secondary"  href="<?php echo url('plugin-store', '', true); ?>" data-method="post" data-json='{"safe_token":"<?php echo $safe_token;?>"}'><?php echo lang('sync_official');?></a>
            <a role="button" class="ajax btn btn-secondary"  href="<?php echo url('plugin-store', '', true); ?>" data-method="post" data-json='{"safe_token":"<?php echo $safe_token;?>","type":"1"}'><?php echo lang('logout');?></a>
        </div>
        <div class="col-8 col-md-6 col-lg-5 input-group px-0">
            <select id="srchtype" value="<?php echo $srchtype; ?>" class="form-control">
                <option value="name"><?php echo lang('plugin');?></option>
                <option value="author" selected><?php echo lang('author');?></option>
            </select>
            <input type="text" id="keyword" placeholder="<?php echo lang('keyword');?>" aria-label="<?php echo $keyword; ?>" value="<?php echo $keyword; ?>"  class="form-control" />
            <div class="input-group-append">
                <button id="search" class="form-control btn btn-primary"><?php echo lang('search');?></button>
            </div>
        </div>
    </div>

    <div class="btn-group btn-group-sm my-3" role="group">
        <?php echo $plugin_cate_html;?>
    </div>

    <?php if ($pluginlist) { ?>
    <div class="threadlist card-columns mb-3">
        <?php foreach($pluginlist as $dir=>$plugin) { ?>

        <?php $plugin['name'] = array_value($plugin, 'name');
		    $plugin['brief'] = array_value($plugin, 'brief');
		    $plugin['version_fmt'] = 'local' == $action ? $plugin['version'] : array_value($plugin, 'official_version');
		?>

        <div class="card">
            <div class="d-flex thread">
                <div class="d-flex align-items-center mx-3">
                    <a data-modal-title="<?php echo $plugin['name']; ?>" data-modal-size="lg" href="<?php echo url('plugin-read', array('dir'=>$dir), TRUE);?>">
                        <img src="<?php echo $plugin['icon_url']; ?>" width="54" height="54" />
                    </a>
                </div>
                <div class="col p-0 m-0">
                    <div class="d-flex justify-content-between">
                        <div class="col px-0 break-all font-weight-bold">
                            <a data-modal-title="<?php echo $plugin['name']; ?>" data-modal-size="lg" href="<?php echo url('plugin-read', array('dir'=>$dir), TRUE);?>">
                                <?php echo $plugin['name']; ?>
                            </a>
                            <span class="small">
                                v<?php echo $plugin['version_fmt']; ?>
                            </span>
                        </div>

                        <div class="mx-2">
                            <?php if($plugin['official']['price'] > 0 && !$plugin['downloaded']) { ?>
                            <a role="button" class="btn btn-danger btn-sm buy mx-1" data-modal-title="<?php echo $plugin['name']; ?>" data-modal-size="lg" href="<?php echo url('plugin-read', array('dir'=>$dir), TRUE);?>"><?php echo lang('buy');?></a>
                            <?php } ?>

                            <?php if(0 == $plugin['official']['price'] && !$plugin['downloaded']) { ?>
                            <a role="button" class="btn btn-primary btn-sm download mx-1" href="<?php echo url('plugin-download', array('dir'=>$dir,'safe_token' => $safe_token), TRUE); ?>"><?php echo lang('download');?></a>
                            <?php } ?>

                            <?php if(!$plugin['installed'] && $plugin['downloaded']) { ?>
                            <a role="button" class="btn btn-success btn-sm mx-1" href="<?php echo url('plugin-install', array('dir'=> $dir, 'safe_token' => $safe_token), TRUE); ?>"><?php echo lang('install');?></a>
                            <?php } ?>

                            <?php if($plugin['installed'] && $plugin['enable']) { ?>
                            <a id="disable" role="button" class="btn btn-secondary btn-sm disable mx-1" href="<?php echo url('plugin-disable', array('dir'=> $dir, 'safe_token' => $safe_token), TRUE); ?>"><?php echo lang('disable');?></a>
                            <?php } ?>

                            <?php if($plugin['installed'] && $plugin['enable'] && $plugin['have_upgrade']) { ?>
                            <a role="button" class="btn btn-danger btn-sm upgrade mx-1" href="<?php echo url('plugin-upgrade', array('dir'=> $dir, 'safe_token' => $safe_token), TRUE); ?>"><?php echo lang('update');?></a>
                            <?php } ?>
                        </div>
                    </div>

                    <div class="d-flex justify-content-between mt-2 mr-2">
                        <div>
                            <i class="icon-rmb mx-1"></i><?php echo $plugin['official']['price']; ?>
                        </div>

                        <div class="small">
                            <?php echo $plugin['stars_fmt']; ?>
                        </div>

                        <div class="small text-muted">
                            <?php echo $plugin['downloads']; ?><span class="ml-1"><?php echo lang('installs');?></span>
                        </div>
                    </div>

                </div>
            </div>

            <div class="grey break-all p-2">
                <?php echo $plugin['brief']; ?>
                <hr>
                <div class="d-flex justify-content-between">
                    <div class="small text-muted">
                        <?php echo lang('author'); ?>：
                        <?php if(!empty($plugin['official']['author'])) { ?>
                        <a href="http://www.wellcms.cn/user-<?php echo $plugin['uid'];?>.html" target="_blank"><?php echo $plugin['author'];?></a>
                        <?php } else { echo lang('none');}?>
                    </div>
                    <div class="text-muted small">
                        Last Update<span class="ml-1"><?php echo $plugin['last_update_fmt']; ?></span>
                        <br>
                        <span class="<?php if($plugin['have_upgrade']) { echo 'text-danger'; } ?>">
                            <?php echo lang('official_version');?><span class="mx-1">v</span><?php echo array_value($plugin, 'official_version'); ?>
                        </span>
                    </div>

                </div>
            </div>
        </div>

        <?php } ?>
    </div>

    <?php if($pagination) { ?>
    <nav>
        <ul class="pagination justify-content-center">
            <?php echo $pagination; ?>
        </ul>
    </nav>
    <?php } ?>

    <?php } else { ?>
    <article class="d-flex justify-content-center thread my-3">
        <?php echo lang('none');?>
    </article>
    <?php } ?>
    <?php } ?>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
    <?php if ('store' == $action && FALSE === $data_verify) { ?>
    var jform = $('#form');
    jform.on('submit', function () {
        jform.reset();
        var jthis = $(this);
        var jsubmit = jthis.find('#submit');
        jsubmit.button('loading');
        var postdata = jform.serializeObject();
        $.xpost(jform.attr('action'), postdata, function (code, message) {
            if (code == 0) {
                jsubmit.button(message).delay(1000).location();
            } else if (xn.is_number(code)) {
                $.alert(message);
                jsubmit.button('reset');
            } else {
                jform.find('[name="' + code + '"]').alert(message).focus();
                jsubmit.button('reset');
            }
        });
        return false;
    });
    <?php } ?>

    var jsearch = $("#search");
    var jsrchtype = $("#srchtype");
    var jkeyword = $('#keyword');
    jsrchtype.val(jsrchtype.attr('value'));

    jsearch.on('click', function () {
        var srchtype = jsrchtype.val();
        var keyword = $("#keyword").val();
        window.location = xn.url('plugin-store', {srchtype: srchtype, keyword: xn.urlencode(keyword)}, true);
    });
    jkeyword.on('keydown', function (e) {
        if (e.keyCode == 13) jsearch.trigger('click');
    });

    $('#plugin').addClass('show');
    $('li.plugin, .menu-store').addClass('active').find('a').removeClass('collapsed');
</script>